/**
 * 
 */
package com.ntv.persistent.news.sql.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.ntv.domain.INewsType;
import com.ntv.persistent.AbstractDao;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.news.INewsTypeDao;

/**
 * @author Vedamoorthi Vairam
 * @since 05/01/2009
 *
 */
public class SqlNewsTypeDao extends AbstractDao implements INewsTypeDao 
{

	private static final Logger logger = Logger.getLogger(NewsDao.class);

	
	interface CONST 
	{
		interface QUERY 
		{
			final String GET_NEWS_TYPE = "SELECT * FROM NEWS_TYPES";

			final String GET_LATEST_NEWS_TYPE_QUEUEY = "SELECT " +
				"TYPE_ID, NAME, DESCRIPTION CREATED_BY, DATE_OF_CREATION, UPDATED_BY, DATE_OF_UPDATION FROM NEWS_TYPE a where TYPE_ID:typeId";
	
		}
	}
	
	public List<INewsType> getNewsTypes() throws PersistentException{
		try 
		{
			return getNamedParameterJdbcTemplate().query(CONST.QUERY.GET_NEWS_TYPE, new HashMap(), getRowMapper());
		} 
		catch (DataAccessException ex) 
		{
			throw new PersistentException(ex);
		}
	}
	
	/* This method is used to retrieve the latest news
	 * The logic is retrieve the message by publish date <last 3 hrs message ordered by published on >
	 * @see com.ntv.persistent.news.INewsDao#getLaststNewsList()
	 */
	public INewsType findNewsType(Long typeId) throws PersistentException 
	{
		try
		{
			SqlParameterSource namedParameters = new MapSqlParameterSource("typeId",typeId);
			return (INewsType)getNamedParameterJdbcTemplate().queryForObject(CONST.QUERY.GET_LATEST_NEWS_TYPE_QUEUEY, namedParameters, getRowMapper());
		} 
		catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
}
